package org.eae.eldf.service.base.impl;

import java.util.Date;

import org.eae.eldf.dao.base.IPortalCompanyUserDao;
import org.eae.eldf.dao.base.impl.PortalCompanyUserDaoImpl;
import org.eae.eldf.dto.base.PortalCompanyUserBean;
import org.eae.eldf.dto.base.PortalCompanyUserForm;
import org.eae.eldf.dto.base.PortalCompanyUserQueryDto;
import org.eae.eldf.entity.company.PortalCompanyUser;
import org.eae.eldf.framework.exception.ServiceException;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.framework.persistence.Criteria;
import org.eae.eldf.framework.service.BaseServiceImpl;
import org.eae.eldf.framework.utils.MD5;
import org.eae.eldf.service.base.PortalCompanyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class PortalCompanyUserServiceImpl extends BaseServiceImpl<PortalCompanyUser> implements PortalCompanyUserService{

	private IPortalCompanyUserDao portalCompanyUserDao;
	
	@Autowired
	public void setPortalCompanyUserDao(PortalCompanyUserDaoImpl portalCompanyUserDao) {
		this.portalCompanyUserDao = portalCompanyUserDao;
	}



	@Override
	public Pagination<PortalCompanyUserBean> queryList(Pagination<PortalCompanyUserBean> pager, PortalCompanyUserQueryDto portalCompanyUserQueryDto) {
		// TODO Auto-generated method stub
		return portalCompanyUserDao.queryListBaseCompanyInfo(pager, portalCompanyUserQueryDto);
	}



	@Override
	public void addUser(PortalCompanyUserForm user)throws ServiceException {
		
		if(!user.getPwd().equals(user.getConPwd())){
			throw new ServiceException("1001");
		}
		//校验companyCode下的username是否存在
		int count = portalCompanyUserDao.queryCount(Criteria.create(PortalCompanyUser.class).
				where("companyCode", new Object[]{user.getCompanyCode()}).
				and("username", new Object[]{user.getUsername()}));
		if(count>0){
			throw new ServiceException("1002");
		}
		
		PortalCompanyUser u = new PortalCompanyUser();
		u.setCompanyCode(user.getCompanyCode());
		u.setEmail(user.getEmail());
		u.setIsDelete(0);
		u.setState(user.getState());
		u.setPhone(user.getPhone());
		u.setRealName(user.getRealName());
		u.setUsername(user.getUsername());
		u.setPwd(MD5.encode(user.getPwd()));
		u.setCreateTime(new Date());
		portalCompanyUserDao.insert(u);
	}



	

}
